import { ajax, cardCache, toast, formDownload, getBusinessInfo } from 'nc-lightapp-front';
import { BUTTONID, AREA, FIELDS, URL } from '../../constance';
import backBtnClick from './backBtnClick';
import addBtnClick from './addBtnClick';
import commitBtnClick from './commitBtnClick';
import unCommitBtnClick from './unCommitBtnClick';
import editBtnClick from './editBtnClick';
import refreshBtnClick from './refreshBtnClick';
import deleteBtnClick from './deleteBtnClick';
import cancelBtnClick from './cancelBtnClick';
import copyBtnClick from './copyBtnClick';
import saveBtnClick from './saveBtnClick';
import addLineBtnClick from './addLineBtnClick';
import deleteLineBtnClick from './deleteLineBtnClick';
import copyLineBtnClick from './copyLineBtnClick';
import pasteLineBtnClick from './pasteLineBtnClick';
import cancelBBtnClick from './cancelBBtnClick';
import insertLineBtnClick from './insertLineBtnClick';
import { create } from 'sortablejs';

let { setDefData, getDefData } = cardCache;


// 表头按钮区操作
function buttonClick(props, id) {
        switch (id) {
                case BUTTONID.Add:
                        addBtnClick.call(this, props);
                        break;
                case BUTTONID.Commit:
                        commitBtnClick.call(this, props);
                        break;
                case BUTTONID.UnCommit:
                        unCommitBtnClick.call(this, props);
                        break;
                case BUTTONID.Edit:
                        editBtnClick.call(this, props);
                        break;
                case BUTTONID.Delete:
                        deleteBtnClick.call(this, props);
                        break;
                case BUTTONID.Copy:
                        copyBtnClick.call(this, props);
                        break;
                case BUTTONID.Refresh:
                        refreshBtnClick.call(this, props);
                        break;
                case BUTTONID.Save:
                        saveBtnClick.call(this, props);
                        break;
                case BUTTONID.Cancel:
                        cancelBtnClick.call(this, props);
                        break;
                case BUTTONID.Back:
                        backBtnClick.call(this, props);
                        break;
                case BUTTONID.Voucher:
                        createVoucher(props);
                        break;
                case BUTTONID.RedHandle:
                        redHandle(props);
                        break
        }
}

// 表体按钮操作
function lineBtnClick(moduleId, props, id, text, record, index) {
        switch (id) {
                case BUTTONID.cardBody_1_AddLine:
                case BUTTONID.cardBody_2_AddLine:
                        addLineBtnClick.call(this, props, moduleId);
                        break;

                case BUTTONID.cardBody_1_DeleteLine:
                case BUTTONID.cardBody_2_DeleteLine:
                        deleteLineBtnClick.call(this, props, moduleId, record, index);
                        break;
                case BUTTONID.cardBody_1_CopyLine:
                case BUTTONID.cardBody_2_CopyLine:
                        copyLineBtnClick.call(this, props, moduleId);
                        break;

                case BUTTONID.cardBody_1_PasteLineToTail:
                case BUTTONID.cardBody_1_PasteLine:
                case BUTTONID.cardBody_2_PasteLineToTail:
                case BUTTONID.cardBody_2_PasteLine:
                        pasteLineBtnClick.call(this, props, moduleId, record, index);
                        break;

                case BUTTONID.cardBody_1_CancelB:
                case BUTTONID.cardBody_2_CancelB:
                        cancelBBtnClick.call(this, props, moduleId);
                        break;

                case BUTTONID.cardBody_1_InsertLine:
                case BUTTONID.cardBody_2_InsertLine:
                        insertLineBtnClick.call(this, props, moduleId, record, index);
                        break;
                case BUTTONID.cardBody_1_Upload:
                case BUTTONID.cardBody_2_Upload:
                        ImportFile(props, moduleId);
                        break;
                case BUTTONID.cardBody_1_DownTemplet:
                case BUTTONID.cardBody_2_DownTemplet:
                        downTemplet(props, moduleId);
                        break;


        }
}


function redHandle(props) {
        
        let pk = props.form.getFormItemsValue(AREA.cardHead, FIELDS.pk_head_fields) && props.form.getFormItemsValue(AREA.cardHead, FIELDS.pk_head_fields).value;
        let data = {
                pk:pk,
                ts: props.form.getFormItemsValue(AREA.cardHead, FIELDS.ts).value,
                action:'redHandle'
        }
        ajax({
                url: URL.Voucher,
                data: data,
                success: (res) => {
                        if (res.data.isSuccess === "Y") {
                                refreshBtnClick.call(this, props);
                                toast({ content: '红冲成功', color: 'success' });
                        } else {
                                toast({ content: res.data.errorMessage, color: 'danger' });//错误消息
                        }

                }
        })
}
function createVoucher(props) {
        
        let pk = props.form.getFormItemsValue(AREA.cardHead, FIELDS.pk_head_fields) && props.form.getFormItemsValue(AREA.cardHead, FIELDS.pk_head_fields).value;
        let data = {
                ts: props.form.getFormItemsValue(AREA.cardHead, FIELDS.ts).value,
                pk:pk,
                action:'Voucher'
        }
        ajax({
                url: URL.Voucher,
                data: data,
                success: (res) => {
                        if (res.data.isSuccess === "Y") {
                                refreshBtnClick.call(this, props);
                                toast({ content: '生成凭证成功', color: 'success' });
                        } else {
                                toast({ content: res.data.errorMessage, color: 'danger' });//错误消息
                        }

                }
        })
}

function ImportFile(props, moduleId) {
        let value = props.form.getFormItemsValue(AREA.cardHead, 'pk_org');
        let pk_org = value.value;
        let center = props.form.getFormItemsValue(AREA.cardHead, 'vdef1').value;
        if (pk_org == null || center == null || center == "") {
                toast({ color: 'danger', content: "请先选择表头利润中心" });
                return;
        }

        setDefData("importID", "importID", moduleId);
        setDefData("importID", "pk_org", pk_org);
        setDefData("importID", "center", center);
        document.querySelector('#fileInfo').click();
}

function selectFileCallBack(formData, props) {

        let url = '/nccloud/manualcostvo/common/importBVO.do';
        let moduleId = getDefData("importID", "importID");
        let pk_org = getDefData("importID", "pk_org");
        let center = getDefData("importID", "center");
        // formData.pk_org = pk_org
        if (moduleId == AREA.cardBody_2) {
                url = '/nccloud/manualcostvo/common/importCVO.do';
        }
        ajax({
                url: url,
                data: formData,
                headers: { 'Content-Type': 'multipart/form-data', pk_org: pk_org, center: center },
                success: (res) => {
                        let _this = this
                        if (res && res.success) {
                                debugger;
                                document.querySelector('#fileInfo').value = ''
                                if (res.data['isSuccess'] === "Y") {
                                        let retdata = res.data;
                                        importSuccess.call(this, props, retdata);
                                } else {
                                        toast({ color: 'danger', content: res.data['errorMessage'] });
                                }
                        }
                }
        })
}

function importSuccess(props, data) {
        debugger;
        let moduleId = getDefData("importID", "importID");
        if(data['retData'].bodys[moduleId]){
                props.cardTable.setTableData(moduleId, { rows: data['retData'].bodys[moduleId].rows });
        }else{
                toast({ content: '无有效数据', color: 'danger' });//错误消息
                return;
        }
       
        if (data.errorMessage == undefined) {
                toast({ content: '导入成功', color: 'success' });
        } else {
                toast({ content: data.errorMessage, color: 'danger' });//错误消息
        }
}

function downTemplet(props, moduleId) {
        let colunmNames = null;
        let fileNames = null;
        if (moduleId == AREA.cardBody_1) {
                colunmNames = ['序号', '员工号','GHR签约法人编号', '签约缴金法人公司', 'GHR机构编码', 'GHR机构', 'GHR实际服务法人编码', '实际服务法人公司',
                        'GHR一级部门编码', 'GHR一级部门名称', 'GHR二级部门编码', 'GHR二级部门名称', '姓名',   'GHR岗位编码', '岗位', '应发工资', '社保', '公积金','六项专项扣除', '所得税', '安全基金', '赔款', '罚款', '话费', '其它', '合计', '实发工资'
                ];
                fileNames = ['人工成本子表']
        } else {
                colunmNames = ['人员工号', 'GHR签约法人编号', '签约缴金法人公司', 'GHR机构编码', 'GHR机构', 'GHR实际服务法人编码', '实际服务法人公司', '个人扣款金额', '投诉单号'];
                fileNames = ['赔款明细']
        }

        // let colunmNames = ['利润中心','明细摘要','金额'];
        let data = {
                colunmNames: colunmNames,
                fileNames: fileNames
        }

        formDownload({
                params: data,
                url: '/nccloud/manualcostvo/common/exportTemplet.do',
                enctype: 2
        });
        toast({ content: '导出成功', color: 'success' });
}

export { buttonClick, lineBtnClick, importSuccess, selectFileCallBack };
